package com.umer.pinban.user;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.db.UserDao;
import com.common.okhttp.OkSimpleRequest;
import com.common.utils.AntiShake;
import com.common.utils.Constants;
import com.common.utils.EventConstants;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.utils.CountDownTimer;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改密码
 */

public class UpDateUserPassWordActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.linear_code)
    LinearLayout linearCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn)
    Button btn;
    private UserInfo userInfo;
    private UserDao userDao;

    CountDownTimer timer1 = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvCode.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tvCode.setText("重新获取验证码");
        }
    };

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_update_user_password;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        userDao = new UserDao(this);
        if (userInfo != null) {
            tvPhone.setText("" + userInfo.getPhone());
        }
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back, R.id.btn, R.id.tv_code})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                finish();
                break;
            //确认修改
            case R.id.btn:
                if (TextUtils.isEmpty(etCode.getText().toString())) {
                    showToast("请输入验证码");
                } else if (TextUtils.isEmpty(etPassword.getText().toString())) {
                    showToast("请输入新密码");
                }  else {
                    updateUserInfo();
                }
                break;
            case R.id.tv_code:
                if (TextUtils.isEmpty(tvPhone.getText().toString().trim())) {
                    showToast("请输入手机号码");
                } else {
                    timer1.start();
                    OkSimpleRequest request = new OkSimpleRequest<>(R.id.authPhone,
                            Constants.authPhone);
                    request.addParams("phone", tvPhone.getText().toString().trim());
                    request.addParams("type", "updateShop");
                    requestOkhttpSimple(request);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.updatePassword:
                hideProgress();
                showToast(t.getDesc());
                AppHolder.getInstance().getUserInfo().setPassword(etPassword.getText().toString());
                userDao.update(AppHolder.getInstance().getUserInfo());
                finish();
                break;
            case R.id.authPhone:
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.updatePassword:
                hideProgress();
                showToast(t.getDesc());
                break;
            case R.id.authPhone:
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    private void updateUserInfo() {
        showProgress("正在修改,请稍后...");
        OkSimpleRequest<InfoResult> request = new OkSimpleRequest<>(R.id.updatePassword,
                Constants.updatePassword);
        request.addParams("authCode", etCode.getText().toString());
        request.addParams("phone", userInfo.getPhone());
        request.addParams("password", etPassword.getText().toString());
        request.setHeader(true);
        requestOkhttpSimple(request);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer1 != null) {
            timer1.cancel();
        }
    }
}
